import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
import json

class Testresourcequota:

    @pytest.mark.parametrize('resourcequotainfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/resourcequota/1_get_resourcequota_list.yaml'))
    def test_get_resourcequota_list(self, resourcequotainfo, get_env_info):
        # 获取resourcequota列表接口
        baseUrl = get_env_info
        url = baseUrl + resourcequotainfo['request']['url'].replace('{namespace}','default')
        method = resourcequotainfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == resourcequotainfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('resourcequotainfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/resourcequota/2_create_resourcequota.yaml'))
    def test_create_resourcequota(self, resourcequotainfo, get_env_info):
        # 创建resourcequota接口
        baseUrl = get_env_info
        url = baseUrl + resourcequotainfo['request']['url'].replace('{namespace}','default')
        method = resourcequotainfo['request']['method']
        data = resourcequotainfo['request']['data']
        req = RequestUtil().send_request(method, url,json=data)
        if '-' not in resourcequotainfo['name']:
            resourcequotaname=req.json()['metadata']['name']
            globals()['resourcequotaname']=resourcequotaname
        assert req.status_code == resourcequotainfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('resourcequotainfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/resourcequota/3_get_specific_resourcequota.yaml'))
    def test_get_specific_resourcequota(self, resourcequotainfo, get_env_info):
        # 获取指定resourcequota接口
        baseUrl = get_env_info
        url = baseUrl + resourcequotainfo['request']['url'].replace('{namespace}','default').replace('{resourcequotaname}',globals()['resourcequotaname'])
        method = resourcequotainfo['request']['method']
        req = RequestUtil().send_request(method, url)
        if '-' not in resourcequotainfo['name']:
            updatereqbody=req.json()
            globals()['updatereqbody']=updatereqbody
        assert req.status_code == resourcequotainfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('resourcequotainfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/resourcequota/4_update_resourcequota.yaml'))
    def test_update_resourcequota(self, resourcequotainfo, get_env_info):
        # 更新指定resourcequota接口
        baseUrl = get_env_info
        url = baseUrl + resourcequotainfo['request']['url'].replace('{namespace}','default').replace('{resourcequotaname}',globals()['resourcequotaname'])
        method = resourcequotainfo['request']['method']
        resourcequotainfo['request']['data'] = globals()['updatereqbody']
        data = resourcequotainfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == resourcequotainfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('resourcequotainfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/resourcequota/5_update_section_resourcequota.yaml'))
    def test_update_section_resourcequota(self, resourcequotainfo, get_env_info):
        # 部分更新指定resourcequota的接口
        baseUrl = get_env_info
        url = baseUrl + resourcequotainfo['request']['url'].replace('{namespace}','default').replace('{resourcequotaname}',globals()['resourcequotaname'])
        method = resourcequotainfo['request']['method']
        data = resourcequotainfo['request']['data']
        header = resourcequotainfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header,json=data)
        assert req.status_code == resourcequotainfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('resourcequotainfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/resourcequota/6_delete_specific_resourcequota.yaml'))
    def test_delete_specific_resourcequota(self, resourcequotainfo, get_env_info):
        # 删除resourcequota接口
        baseUrl = get_env_info
        url = baseUrl + resourcequotainfo['request']['url'].replace('{namespace}','default').replace('{resourcequotaname}',globals()['resourcequotaname'])
        method = resourcequotainfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == resourcequotainfo['validate'][0]['equals']['status_code']